package cn.a7e7.idanalysis;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * 操作数据库方法的类
 */
public class DaoSQLiteDB {

    private static final String TAG = "ERRRRR";
    private final DatabaseHelper helper;

    // 创建数据库
    public DaoSQLiteDB(Context context) {
        helper = new DatabaseHelper(context);
    }


    /**
     * 查询2位地址码的信息
     *
     * @param a 传入两位的地址码
     * @return 返回地址信息
     */
    public String queryAddressCode2(int a) {
// 打开数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        // 改成查询的SQL语句
        // select AreaCode,UnitName from AreaCodeTable where ProvinceCode = a;
        String sql = "select " + Constants.DATA_02 + "," + Constants.DATA_06 + " from " + Constants.DATABASE_TABLE_NAME + " where " + Constants.DATA_03 + " = " + a + ";";
        // 执行查询语句并获取查到的内容
        Cursor cursor = db.rawQuery(sql, null);
        // 看看查询的结果是否为空
        if (cursor == null) {
            return Constants.SELECT_NULL;
        }
        // 声明一个字符串来存东西（我记得有另一个更好的方法）
        StringBuilder outStr = new StringBuilder();
        while (cursor.moveToNext()) {
            outStr.append(cursor.getInt(0)).append("   ").append(cursor.getString(1)).append("\n");
        }
        cursor.close();  // 用完关闭cursor
        db.close();      // 用完关闭数据库
        return outStr.toString();            // 返回查询的内容（整理后的）

    }

    /**
     * 查询4位地址码的信息
     *
     * @param a 传入4位的地址码
     * @return 返回地址信息
     */
    public String queryAddressCode4(int a, int b) {
        SQLiteDatabase db = helper.getWritableDatabase();
        // 改成查询的SQL语句
        // select AreaCode,UnitName from AreaCodeTable where ProvinceCode = a;
        String sql = "select " + Constants.DATA_02 + "," + Constants.DATA_06 + " from " + Constants.DATABASE_TABLE_NAME + " where " + Constants.DATA_03 + " = " + a + " AND " + Constants.DATA_04 + " = " + b + ";";
        // 执行查询语句并获取查到的内容
        Cursor cursor = db.rawQuery(sql, null);
        // 看看查询的结果是否为空
        if (cursor == null) {
            return Constants.SELECT_NULL;
        }
        // 声明一个字符串来存东西（我记得有另一个更好的方法）
        StringBuilder outStr = new StringBuilder();
        while (cursor.moveToNext())
            outStr.append(cursor.getInt(0)).append("   ").append(cursor.getString(1)).append("\n");
        cursor.close();  // 用完关闭cursor
        db.close();      // 用完关闭数据库
        return outStr.toString();            // 返回查询的内容（整理后的）
    }

    /**
     * 查询6位地址码的信息
     *
     * @param a 传入6位的地址码
     * @return 返回地址信息
     */
    public String queryAddressCode6(int a) {
        SQLiteDatabase db = helper.getWritableDatabase();
        // 改成查询的SQL语句
        // select AreaCode,UnitName from AreaCodeTable where ProvinceCode = a;
        String sql = "select " + Constants.DATA_02 + "," + Constants.DATA_06 + " from " + Constants.DATABASE_TABLE_NAME + " where " + Constants.DATA_02 + " = " + a + ";";
        // 执行查询语句并获取查到的内容
        Cursor cursor = db.rawQuery(sql, null);
        // 看看查询的结果是否为空
        if (cursor == null) {
            return Constants.SELECT_NULL;
        }
        // 声明一个字符串来存东西（我记得有另一个更好的方法）
        StringBuilder outStr = new StringBuilder();
        while (cursor.moveToNext()) {
            outStr.append(cursor.getInt(0)).append("   ").append(cursor.getString(1)).append("\n");
        }
        cursor.close();  // 用完关闭cursor
        db.close();      // 用完关闭数据库
        return outStr.toString();            // 返回查询的内容（整理后的）
    }

    /**
     * 查询地名的信息
     *
     * @param str 一个地名
     * @return 返回地址信息
     */
    public String queryPlaceName(String str) {
        SQLiteDatabase db = helper.getWritableDatabase();
        // 改成查询的SQL语句
        // select AreaCode,UnitName from AreaCodeTable where ProvinceCode = a;
        String sql = "select " + Constants.DATA_02 + "," + Constants.DATA_06 + " from " + Constants.DATABASE_TABLE_NAME + " where " + Constants.DATA_06 + " like '%" + str + "%';";
        // 执行查询语句并获取查到的内容
        Cursor cursor = db.rawQuery(sql, null);
        // 看看查询的结果是否为空
        if (cursor == null) {
            return Constants.SELECT_NULL;
        }
        // 声明一个字符串来存东西（我记得有另一个更好的方法）
        StringBuilder outStr = new StringBuilder();
        while (cursor.moveToNext()) {
            outStr.append(cursor.getInt(0)).append("   ").append(cursor.getString(1)).append("\n");
        }
        cursor.close();  // 用完关闭cursor
        db.close();      // 用完关闭数据库
        return outStr.toString();            // 返回查询的内容（整理后的）
    }

    /**
     * 传入一个小于3000的id查询对应的6位地区码
     *
     * @param id 地区码id
     * @return 6位地区码
     */
    public String seletCityCode(int id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "select " + Constants.DATA_02 + " from " + Constants.DATABASE_TABLE_NAME + " where " + Constants.DATA_01 + " = " + id + ";";
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery(sql, null);
        int a = 0;
        while (cursor.moveToNext()) {
            a = cursor.getInt(0);
        }
//        Log.d(TAG, "id=" + id + ",id查询得内容" + a);
        return String.valueOf(a);
    }


}
